package com.itcast.bihuan;

import com.itcast.util.ExcelUtils;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;

/**
 * Created by Administrator on 2016/3/15 0015.
 */
public class LuShunyueGuanbin {

    /**
     * 递归读文件夹中的文件，并且把文件写入到一个csv文件中
     * @param args
     */
    public static void main(String[] args) {

        String keyPath = "d:\\js格式未闭环竞标标讯20160314.xlsx";
        List<List<Object>> list = getKeyWords(keyPath);
        String path = "e:\\baidu\\All";
        String ffp = "e:/zuizhong";
        String fileName = "bihuan.csv";
        File fileses = new File(ffp,fileName);
        int i =0;
        try {
            test(i,path,list,fileses);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


    public static List<List<Object>> getKeyWords(String path){
        List<List<Object>>  list =  ExcelUtils.readExcelAll(path,1);
        return list;
    }


    public static void test (int i,String path,List<List<Object>> lists,File fileses) throws IOException {

        File f = new File(path);

        File[] fs = f.listFiles();

        if (fs == null) {
            return;
        }
        for (File file : fs) {
            if (file.isFile()) {
                String strContent = FileUtils.readFileToString(file,"utf-8");
                for(List<Object> lis : lists){
                    if(strContent.contains(lis.get(1).toString())){
                        i++;
                        //写入编号
                        CatchLinks.writeElement(fileses,lis.get(1).toString());
                        List<String> fileContentList = FileUtils.readLines(file);
                        int m = 0;
                        for(String str : fileContentList) {
                            if(m>0 && m <3) {
                                CatchLinks.writeElement(fileses, str);
                            }
                            m++;
                            if(m == 3){
                                break;
                            }
                        }
                        try {
                            FileUtils.write(fileses, "\r\n", "GBK", true);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        System.out.println(i);
                    }
                }
            } else {
                test(i,file.getPath(),lists,fileses);
            }
                //拿到这个文件file就是需要获取的文件
               // List<List<Object>>  list =  ExcelUtils.readExcelAll(file.getPath(),1);
//                for(List<Object> lis : list){
//                    for(List<Object> liss :  lists){
//                        if(lis.get(5).toString().contains(liss.get(1).toString())){
//                            System.out.println(file);
//                            System.out.println(lis.get(5));
//                            System.out.println(liss.get(1));
//                            i++;
//                            //写一个文件
//                            CatchLinks.writeElement(fileses,liss.get(1).toString());
//                            CatchLinks.writeElement(fileses,lis.get(2).toString());
//                            CatchLinks.writeElement(fileses,lis.get(3).toString());
//                            try {
//                                FileUtils.write(fileses, "\r\n", "GBK", true);
//                            } catch (IOException e) {
//                                e.printStackTrace();
//                            }
//                            //System.out.println(i);
//                        }
//                    }
//                }

        }
    }
}
